<?php
require('../../lib/base.php');
$base=new base(array('web','advertiser'));
$output=array('state'=>'','action'=>strtoupper($base->get('action')),'data'=>'');
$error=array();$ip=$base->server('REMOTE_ADDR');
$api=$base->vars('app','encryption');
$mixed=json_decode('[{"track":"DAC1AE2C7A6171E94B8997CCE17A4BA74B9D90851356425279","order":"301495026","time":"2012-01-25 12:00:0","state":"待付款","name":"天然牛角 保健按摩 梳子","money":159,"number":1,"commision":10},' .
		'{"track":"DAC1AE2C7A6171E94B8997CCE17A4BA792517EAF1356582379","order":"301439501","time":"2012-01-25 12:00:0","state":"待付款","name":"STAR系列简约生活轻便百搭低帮套脚帆布鞋","money":179,"number":1,"commision":10},' .
		'{"track":"DAC1AE2C7A6171E94B8997CCE17A4BA75B304A911356601484","order":"301495026","time":"2012-01-25 12:00:0","state":"待付款","name":"飞天茅台53度 500ML","money":1599,"number":1,"commision":159},' .
		'{"track":"admig","order":"412791295","time":"2012-12-17 16:35:34","state":"待付款","name":"韩伊CO.E橄榄Olive去角质清洁乳 200g去角质死皮细腻皮肤","money":29,"number":1,"commision":2}]');
//$mixed=json_decode('[{"order":"301495026","state":"确认收货"},{"order":"301439501","state":"退货"}]');
//$mixed=json_decode('{"order":"301495026","state":"确认收货"}');
//$mixed=json_decode('[{"order":"279631094"},{"order":"279631094"}]');
$security=$base->get('security');$mixed=json_decode(strtolower($base->post('mixed')));
if(md5($api.$ip)===$security){
	if(empty($mixed)||!(is_array($mixed)||!is_object($mixed))){
		$output['state']='JSON_ERROR';
	}else{
		if(is_object($mixed)){$mixed=array(get_object_vars($mixed));}
		$output['state']='OK';
		if($output['action']=='WRITE'){
			$where='';$account=array();$transform=array();$order=array();
			foreach($mixed as $key=>$value){
				if(is_object($value)){$value=get_object_vars($value);}
				$value['track']=mysql_escape_string($value['track']);
				if(strlen($value['track'])==50){
					$value['visitor']=substr($value['track'],0,32);
					$value['pageview']=intval(substr($value['track'],40,10));
					$value['track']=substr($value['track'],32,8);
					$transform['advertise'][]=array('member'=>0,'order'=>$value['order'],'visitor'=>$value['visitor'],'track'=>$value['track'],'pageview'=>$value['pageview'],
					'name'=>$value['name'],'money'=>$value['money'],'number'=>$value['number'],'rate'=>intval(($value['commision']/$value['number'])/$value['money']*100),
					'commision'=>$value['commision'],'time'=>intval(strtotime($value['time'])),'state'=>$value['state'],'date'=>$base->runtime,'ip'=>$ip);
					$where.="(date={$value['pageview']} AND visitor='{$value['visitor']}' AND track='{$value['track']}') OR ";
				}else{
					$track=explode('.',$value['track']);
					if(count($track)==2&&is_numeric($track[1])){
						$transform['customize'][]=array('member'=>intval($track[1]),'order'=>$value['order'],'visitor'=>$value['track'],'track'=>'','pageview'=>0,
						'name'=>$value['name'],'money'=>$value['money'],'number'=>$value['number'],'rate'=>intval(($value['commision']/$value['number'])/$value['money']*100),
						'commision'=>$value['commision'],'time'=>intval(strtotime($value['time'])),'state'=>$value['state'],'date'=>$base->runtime,'ip'=>$ip);
					}else{
						$value['visitor']=$value['track'];$value['track']='0';$value['pageview']=0;
						$transform['account'][]=array('member'=>0,'order'=>$value['order'],'visitor'=>$value['visitor'],'track'=>$value['track'],'pageview'=>$value['pageview'],
						'name'=>$value['name'],'money'=>$value['money'],'number'=>$value['number'],'rate'=>intval(($value['commision']/$value['number'])/$value['money']*100),
						'commision'=>$value['commision'],'time'=>intval(strtotime($value['time'])),'state'=>$value['state'],'date'=>$base->runtime,'ip'=>$ip);
						$account[]=$value['visitor'];
					}
				}
				$order[]=$value['order'];
			}
			unset($mixed);
			if(empty($transform)){
				$output['state']='FORMAT_ERROR';
				$output['data']=$transform;
			}else{
				$data=$base->model('data');
				$temp=$data->select('transform',array('where'=>array('`order` IN'=>array_unique($order))));
				if(empty($temp)){
					if(isset($transform['advertise'])){
						$where=substr_replace($where,'',-4,4);
						$pageview=$data->select('pageview',array('select'=>'visitor,track,date,billboard','where'=>$where));
						foreach($transform['advertise'] as $key=>$value){
							foreach($pageview as $k=>$v){
								if($v['visitor']==$value['visitor']&&$v['track']==$value['track']&&$v['date']==$value['pageview']){
									$value['member']=$v['billboard'];
									$board[]=$v['billboard'];
									break 1;
								}
							}
							$transform['advertise'][$key]=$value;
						}
						if(isset($board)){
							$billboard=$base->model('billboard');
							$board=$billboard->GBoard(array('where'=>array('id IN'=>array_unique($board))));
						}
						foreach($transform['advertise'] as $key=>$value){
							$transform['advertise'][$key]['member']=isset($board[$value['member']])?intval($board[$value['member']]['member']):0;
						}
					}
					if(isset($transform['account'])){
						$mation=$base->model('information');
						$mation=$mation->GMation(array('select'=>'member,account','where'=>array('account IN'=>array_unique($account))));unset($account);
						foreach($mation as $value){
							$account[$value['account']]=$value['member'];
						}
						foreach($transform['account'] as $key=>$value){
							$transform['account'][$key]['member']=isset($account[$value['visitor']])?intval($account[$value['visitor']]):0;
						}
					}
					$transform['advertise']=empty($transform['advertise'])?array():$transform['advertise'];
					$transform['account']=empty($transform['account'])?array():$transform['account'];
					$transform['customize']=empty($transform['customize'])?array():$transform['customize'];
					$transform=array_merge($transform['advertise'],$transform['account'],$transform['customize']);
					if(empty($transform)){
						$output['state']='DATA NOT FOUND';
					}else{
						$effect=$data->insert('transform',array('field'=>array('member','order','visitor','track','pageview','name','money','number','rate','commision','time','state','date','ip'),'value'=>$transform));
						if($effect==count($transform)){
							$output['state']='OK';
						}else{
							$output['state']='FAIL';
						}
					}
				}else{
					$output['state']='REPEAT';
					$output['data']=implode(',',$order);
				}
			}
		}elseif($output['action']=='UPDATE'){
			foreach($mixed as $key=>$value){
				if(is_object($value)){$value=get_object_vars($value);}
				if(empty($value['order'])){
					$output['state']='FORMAT_ERROR';
					$output['data']=$value;
					break;
				}else{
					$value['order']=mysql_escape_string($value['order']);
					$value['state']=mysql_escape_string($value['state']);
					$mixed[$key]=$value;
				}
			}
			if(!empty($mixed)&&$output['state']=='OK'){
				$data=$base->model('data');
				foreach($mixed as $key=>$value){
					$data->update('transform',array('set'=>array('state='=>$value['state']),'where'=>array('`order`='=>$value['order'])));
				}
			}
		}elseif($output['action']=='READ'){
			foreach($mixed as $key=>$value){
				if(is_object($value)){$value=get_object_vars($value);}
				if(empty($value['order'])){
					$output['state']='FORMAT_ERROR';
					$output['data']=$value;
					break;
				}else{
					$value=mysql_escape_string($value['order']);
					$mixed[$key]=$value;
				}
			}
			if(!empty($mixed)&&$output['state']=='OK'){
				$data=$base->model('data');
				$temp=$data->select('transform',array('where'=>array('`order` IN'=>$mixed)));
				$transform=array();
				foreach($temp as $key=>$value){
					$transform[]=array('order'=>$value['order'],'locked'=>intval($value['locked']));
				}
				$output['data']=$transform;
			}
		}else{
			$output['state']='FAIL';
			$output['action']='NONE';
		}
	}
}else{
	$output['state']='INVALID';
}
header('Content-type:application/json');
echo json_encode($output);
?>